package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.SedeService;
import co.gov.medellin.ssti.sub.model.service.SubprogramaService;
import co.gov.medellin.ssti.sub.model.vo.SedeVO;
import co.gov.medellin.ssti.sub.model.vo.SubprogramaVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;

public class ValidarRenovarAction extends Action {
	@Autowired
	SedeService sedeServiceImpl;
	@Autowired
	SubprogramaService subprogramaServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		HttpSession session = request.getSession();
		UserVO user = (UserVO) session.getAttribute("userInfo");
		String codeBeneficio = request.getParameter("codBeneficio");
		if ((Object) codeBeneficio != null) {
			session.setAttribute("codBeneficio", codeBeneficio);
		} else {
			codeBeneficio = (String) session.getAttribute("codBeneficio");
		}

		try {
			SubprogramaVO subproVO = new SubprogramaVO();
			subproVO.setCode(new Long(codeBeneficio));
			Collection<SubprogramaVO> subpro = subprogramaServiceImpl
					.findSubprogramaByCriteria(subproVO, user.getCode(),
							user.getUserGroupName(), user.getCodeArea());

			if (subpro != null && subpro.size() > 0) {
				try {
					Collection<SedeVO> sedes = sedeServiceImpl
							.findTotalSedebyUser(user.getCode(), new Long(
									codeBeneficio), user.getUserGroupName(),
									user.getCodeArea());

					if ((Object) sedes != null) {
						if (sedes.size() > 0) {
							session.setAttribute("sedes", sedes);
						} else {
							session.setAttribute("msg",
									"No posee permisos para renovar el beneficio en ninguna sede.");
							return mapping.findForward("fail");
						}
					} else {
						session.setAttribute("msg",
								"No posee permisos para renovar el beneficio en ninguna sede.");
						return mapping.findForward("fail");
					}
				} catch (DAOException e) {
					SUBException.logDebugError("", e);
				}

			} else {
				session.setAttribute("msg",
						"El beneficio no se encuentra activo por lo tanto no se puede renovar.");
				return mapping.findForward("fail");
			}
		} catch (DAOException e) {
			SUBException.logDebugError("", e);
		}

		return mapping.findForward("success");
	}
}